package com.tools.controller.user;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tools.common.enums.ResultCode;
import com.tools.common.result.Result;
import com.tools.pojo.user.bo.RoleBO;
import com.tools.pojo.user.dto.RoleDTO;
import com.tools.pojo.user.vo.RoleVO;
import com.tools.service.user.RoleService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/role")
@AllArgsConstructor
@Tag(name = "角色模块",description = "角色模块")
public class RoleController {

    private final RoleService roleService;

    @PostMapping("/add")
    @Operation(summary = "创建角色", description = "添加新角色")
    @ApiResponse(responseCode = "200", description = "角色创建成功")
    public Result<?> createRole(@RequestBody RoleBO roleBO) {
        Boolean result = roleService.createRole(roleBO);
        return result?Result.success():Result.fail(ResultCode.DATA_ADD_ERROR);
    }

    @DeleteMapping("/delete/{id}")
    @Operation(summary = "删除角色", description = "根据ID删除角色")
    public Result<?> deleteRoleById(
            @Parameter(description = "角色ID") @PathVariable("id") Long id) {
        Boolean result = roleService.deleteRoleById(id);
        return result?Result.success():Result.fail(ResultCode.DATA_ADD_ERROR);
    }

    @PostMapping("/update/{id}/{status}")
    @Operation(summary = "修改角色状态", description = "启用/禁用角色")
    public Result<?> updateRoleStatus(
            @Parameter(description = "角色ID") @PathVariable("id") Long id,
            @Parameter(description = "状态值") @PathVariable("status") String status) {
        Boolean result = roleService.updateRoleStatus(id, status);
        return result?Result.success():Result.fail(ResultCode.DATA_ADD_ERROR);
    }

    @PostMapping("/update")
    @Operation(summary = "更新角色信息", description = "修改角色详细信息")
    public Result<?> updateRole(@RequestBody RoleBO roleBO) {
        Boolean result = roleService.updateRole(roleBO);
        return result?Result.success():Result.fail(ResultCode.DATA_ADD_ERROR);
    }

    @GetMapping("/page")
    @Operation(summary = "分页查询角色", description = "获取角色分页列表")
    public Result<IPage<RoleVO>> getRolePage(RoleDTO roleDTO) {
        IPage<RoleVO> rolePage = roleService.getRolePage(roleDTO);
        return Result.success(rolePage);
    }

    @GetMapping("/{id}")
    @Operation(summary = "获取角色详情", description = "根据ID查询角色详细信息")
    public Result<RoleVO> getRoleById(
            @Parameter(description = "角色ID") @PathVariable Long id) {
        RoleVO roleVO = roleService.getRoleById(id);
        return roleVO!=null?Result.success(roleVO):Result.fail(ResultCode.DATA_QUERY_ERROR);
    }

    @PostMapping("/batchDelete")
    @Operation(summary = "批量删除角色", description = "删除多个角色记录")
    public Result<Void> batchDeleteRole(@RequestBody List<Long> ids) {
        Boolean result = roleService.batchDeleteRole(ids);
        return result?Result.success():Result.fail(ResultCode.DATA_DEL_ERROR);
    }

    @GetMapping("/selectList")
    @Operation(summary = "获取角色下拉列表", description = "用于表单选择的角色列表")
    public Result<List<RoleVO>> getRoleSelectList() {
        List<RoleVO> roleSelectList = roleService.getRoleSelectList();
        return roleSelectList!=null?Result.success(roleSelectList):Result.fail(ResultCode.DATA_QUERY_ERROR);
    }

}
